class Solution 
{
public:
    const int size = 10001;
    int deleteAndEarn(vector<int>& nums) 
    {
        
        vector<int> v(size);
        for(auto e : nums) v[e] += e;

        vector<int> g(size), f(size);
        for(int i=1; i<size; i++)
        {
            f[i] = g[i-1] + v[i];
            g[i] = max(f[i-1], g[i-1]);
        }

        return max(f[size-1], g[size-1]);
    }
};